package com.file.system.service.impl.system;

import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.sort.BaseSort;
import com.file.system.entity.system.WebDictionary;
import com.file.system.mapper.system.WebDictionaryMapper;
import com.file.system.plugin.dict.DictCache;
import com.file.system.plugin.dict.DictCacheType;
import com.file.system.service.system.IWebDictionaryService;
import com.file.system.tools.page.DP;
import com.file.system.tools.sort.SortUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static cn.hutool.core.util.ObjectUtil.isEmpty;
import static cn.hutool.core.util.ObjectUtil.isNull;

/**
 * 字典 实现
 *
 * @author jch
 * @since 2022-07-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WebDictionaryServiceImpl implements IWebDictionaryService {

    @Autowired
    private WebDictionaryMapper mapper;
    @Autowired
    private SortUtil sortUtil;

    //添加字典
    @DictCache(type = DictCacheType.dict_put)
    public boolean save(WebDictionary webDictionary) {
        webDictionary.setId(IdUtil.getSnowflakeNextId());
        //排序
        webDictionary = sortUtil.add(webDictionary, webDictionary.getClass(), "web_dictionary",
                BaseSort.eq().val("is_del", 0),
                BaseSort.eq().val("dict_list_or_tree", webDictionary.getDictListOrTree())
        );
        if (isNull(webDictionary)) {
            return false;
        }
        return SqlHelper.retBool(mapper.save(webDictionary));
    }


    //删除字典
    @DictCache(type = DictCacheType.dict_delete)
    public boolean removeById(@Param("id") Long id) {
        //排序
        WebDictionary webDictionary = mapper.selectById(id);
        sortUtil.del(webDictionary, webDictionary.getClass(), "web_dictionary",
                BaseSort.eq().val("is_del", 0),
                BaseSort.eq().val("dict_list_or_tree", webDictionary.getDictListOrTree())
        );
        return SqlHelper.retBool(mapper.removeById(new WebDictionary().setId(id)));
    }


    //编辑字典
    @DictCache(type = DictCacheType.dict_put)
    public boolean updateById(@Param("et") WebDictionary webDictionary) {
        if (isEmpty(webDictionary.getId())) {
            return false;
        }
        WebDictionary webDictionaryOld = mapper.selectById(webDictionary.getId());
        //排序
        webDictionary = sortUtil.update(webDictionary, webDictionary.getClass(), webDictionaryOld.getSort(), "web_dictionary",
                BaseSort.eq().val("is_del", 0),
                BaseSort.eq().val("dict_list_or_tree", webDictionary.getDictListOrTree())
        );
        if (isNull(webDictionary)) {
            return false;
        }
        return SqlHelper.retBool(mapper.updateById(webDictionary));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<WebDictionary> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //添加字典(批量)
    public boolean saveBatch(@Param("list") List<WebDictionary> webDictionaryList) {
        if (webDictionaryList.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(webDictionaryList));
    }


    //查询字典
    @DS("slave")
    public List<WebDictionary> list(@Param("ew") QueryWrapper<WebDictionary> query) {
        return mapper.select(query);
    }

    //通过ids查询字典集合
    @DS("slave")
    public List<WebDictionary> listByIds(List<Long> ids) {
        QueryWrapper<WebDictionary> query = new QueryWrapper<>();
        query.in("a.id", ids);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询所有非删除状态字典
    @DS("slave")
    public List<WebDictionary> listAll() {
        QueryWrapper<WebDictionary> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        query.orderByAsc("a.sort");
        return mapper.select(query);
    }

    //查询字典分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<WebDictionary> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebDictionary> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebDictionary> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //通过id查询字典
    @DS("slave")
    public WebDictionary getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询字典
    @DS("slave")
    public WebDictionary getOne(@Param("ew") QueryWrapper<WebDictionary> query) {
        List<WebDictionary> list = mapper.select(query);
        System.out.println(list);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询字典数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebDictionary> query) {
        return mapper.selectCount(query);
    }
}
